// Copyright 2025 祝灵福
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// fs读写模块的readFileSync：一个阻塞式的同步方法，用于读取文件的内容。 参数(文件的路径,指定读取文件时的选项(字符编码))
import { readFileSync } from 'fs'
import * as yaml from 'js-yaml'
// path模块，路径处理，使用path.join方法，拼接路径
import { join } from 'path'
// 导入lodash
import * as _ from 'lodash'

// 1.配置文件路径
const YAML_COMMON_CONFIG_FILENAME = 'config.yaml' //yarm共有的配置的文件名
// __dirname内置变量(获取当前模块目录-绝对路径)(双下划线)
const filePath = join(__dirname, '../src/config', YAML_COMMON_CONFIG_FILENAME)
// 新增
const envPath = join(
  __dirname,
  '../src/config',
  `config.${process.env.NODE_ENV || 'development'}.yaml`
)
// 2.加载配置
const commonConfig = yaml.load(readFileSync(filePath, 'utf-8')) as Record<string, any>
const envConfig = yaml.load(readFileSync(envPath, 'utf-8')) as Record<string, any>

// 导出，因为ConfigModule有一个load方法=>需要函数形式
export default () => {
  //  lodash中的_.merge: 深度合并多个对象。
  return _.merge(commonConfig, envConfig)
}
